package gui.frames;

import javax.swing.JFrame;
import net.miginfocom.swing.MigLayout;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;

import core.persistance.Profile;

public class ProfilesEditor extends JFrame {
	/**
	 * 
	 */
	private static final long serialVersionUID = 8525760114350814829L;
	private JTextField txtName;
	private JTextField txtUsername;
	private JPasswordField pwdPassword;
	private JTextField txtHostname;
	private JTextField txtPort;
	private JButton btnSave;
	public ProfilesEditor() {
		getContentPane().setLayout(new MigLayout("", "[][100px:150px]", "[][][][][][]"));
		
		JLabel lblName = new JLabel("Profile's Name:");
		getContentPane().add(lblName, "cell 0 0,alignx trailing");
		
		txtName = new JTextField();
		getContentPane().add(txtName, "cell 1 0,growx");
		txtName.setColumns(10);
		
		JLabel lblUsername = new JLabel("Username:");
		getContentPane().add(lblUsername, "cell 0 1,alignx trailing");
		
		txtUsername = new JTextField();
		getContentPane().add(txtUsername, "cell 1 1,growx");
		txtUsername.setColumns(10);
		
		JLabel lblPassword = new JLabel("Password:");
		getContentPane().add(lblPassword, "cell 0 2,alignx trailing");
		
		pwdPassword = new JPasswordField();
		getContentPane().add(pwdPassword, "cell 1 2,growx");
		pwdPassword.setColumns(10);
		
		JLabel lblHostname = new JLabel("Hostname:");
		getContentPane().add(lblHostname, "cell 0 3,alignx trailing");
		
		txtHostname = new JTextField();
		getContentPane().add(txtHostname, "cell 1 3,growx");
		txtHostname.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		getContentPane().add(lblPort, "cell 0 4,alignx trailing");
		
		txtPort = new JTextField();
		getContentPane().add(txtPort, "cell 1 4,growx");
		txtPort.setColumns(10);
		
		btnSave = new JButton("Save");
		getContentPane().add(btnSave, "cell 1 5,alignx right");
	}
	
	public JButton getBtnSave() {
		return this.btnSave;
	}

	public void setTextFields(Profile p) {
		txtName.setText(p.getName());
		txtUsername.setText(p.getUsername());
		pwdPassword.setText(p.getPassword());
		txtHostname.setText(p.getHostname());
		txtPort.setText(Integer.toString(p.getPort()));
	}
	
	public String getName() {
		return txtName.getText();
	}
	
	public String getUsername() {
		return txtUsername.getText();
	}
	
	public String getPassword() {
		return new String(pwdPassword.getPassword());
	}
	
	public String getHostname() {
		return txtHostname.getText();
	}
	
	public int getPort() {
		try {
			return Integer.parseInt(txtPort.getText());
		} catch (NumberFormatException e) {
			return 0;
		}
	}
	
	public void setFocusOnNameTextBox() {
		txtName.requestFocusInWindow();
	}

}
